package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.DraftAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.send.plan.PlanSend;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.po.PlanClient;
import com.thti.mecool.client.util.TimeUtil;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class MyPlanActivity extends BaseActivity{
	ProgressDialogWidget pd;
	private ListView myPlanListView;
	public static int modify = 0;
	List<Map<String, String>> list;
	PlanBizImpl planBizImpl;
	BaseAdapter draftAdapter;
	ArrayList<String> planidList;
	Handler handler;
	private Runnable r=new Runnable() {
		@Override
		public void run() {
			if(getDataFlag(HandlerConstant.MyPlanCommand)){
				handler.removeCallbacks(r);
				if(pd.isShowing()){
					pd.dismiss("获取数据成功！");
					}
				removeDataFlag(HandlerConstant.MyPlanCommand);
				Sqlinit();
			}else{ 
				if(getDataFlag(HandlerConstant.CommandFail)){
					handler.removeCallbacks(r);
					if(pd.isShowing())
						pd.dismiss("获取失败！");
					removeDataFlag(HandlerConstant.CommandFail);
				}else
					handler.postDelayed(r, 1000);
			}
		}
	};
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	noTitle();
	setContentView(R.layout.mecool_draft_plan);
	Cache.add(this);
	initToolbar();
	handler=new Handler();
	myPlanListView = (ListView) findViewById(R.id.mecool_draft_plan_list);
	planBizImpl = new PlanBizImpl(this);
	init();
	PlanSend.getMyPlan(getCurrentUser().getLoginName(), TimeUtil.dateToString(new Date(), 4));
	handler.post(r);
	pd=new ProgressDialogWidget(this);
}
public void init(){
	list = new ArrayList<Map<String, String>>();
	planidList = new ArrayList<String>();
	Sqlinit();
}
private void Sqlinit() {
	list.clear();
	planidList.clear();
	List<PlanClient> planlist = planBizImpl.findMyPlanAll(String.valueOf(getCurrentUser().getId()));
	System.out.println("planSize ::    "+planlist.size());
	for (PlanClient plan : planlist) {
		if (plan.getIsType() == 2) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", plan.getPlanName());
			map.put("time", plan.getPlanTime());
			System.out.println(plan.getPlanName());
			list.add(map);
			planidList.add(plan.getPlanId());
		}
	}
	service();
}

private void service() {
	if(draftAdapter!=null){
		draftAdapter.notifyDataSetChanged();
	}else{
	draftAdapter = new DraftAdapter(this, list);
	myPlanListView.setAdapter(draftAdapter);
	myPlanListView.setOnItemClickListener(onItemClickListener);
	}
}
private void initToolbar() {
	Toolbar toolbar = new Toolbar(this);
	toolbar.setToolbarCentreText("我的计划");
	Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	Toolbar.hideRightButton();
	Toolbar.left_btn.setOnClickListener(toolbarListener);
}

private OnItemClickListener onItemClickListener=new OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent i=new Intent(MyPlanActivity.this,MyPlanTaskActivity.class);
		i.putExtra("planId", planidList.get(arg2));
		startActivity(i);
	}
};

private OnClickListener toolbarListener = new ImageButton.OnClickListener() {
	@Override
	public void onClick(View v) {
		finish();
	}
};

@Override
protected void onResume() {
	super.onResume();
	Sqlinit();
};

}
